package com.hoolai.qps.netty.proto;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;

import com.hoolai.qps.bo.ReqCmdProtoFactory;

public class LoopClient extends Client {

    public static void main(String[] args) {
        try {
            new LoopClient(new InetSocketAddress("127.0.0.1", 2222));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public LoopClient(InetSocketAddress address) throws InterruptedException {
        ClientBootstrap bootstrap = this.getBootstrap(address);
        ExecutorService es = Executors.newCachedThreadPool();
        for (int i = 0; i < 2000; i++) {
            System.err.println(i);
            final ChannelFuture future = bootstrap.connect();// connect
            // proxy
            final Channel channel = future.awaitUninterruptibly().getChannel();
            es.submit(new Runnable() {
                @Override
                public void run() {
                    for (;;) {
                        ReqCmdProtoFactory potoFactory = new ReqCmdProtoFactory();
                        channel.write(new Cmd(potoFactory.getReqCmdProtoByCmdId(1000002),1000002));
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            Thread.sleep(10);
        }
    }

}
